/*******************************************************************
bstDriver class

Cermello Woodgett and Zach Smith

This class runs and tests major BST functions.
*******************************************************************/
import java.util.*;
import java.io.*;
import java.util.Random;

public class bstDriver 
{    
    public static void main(String[] args) 
    {
        /*****variable declaration*****/
        BST<Item, Integer> binTree = new BST<Item, Integer>();
            //an instance of BST using Item(an Integer based implementation of the 
            //abstract class KeyedItem)
        String fileName = "project 2 file.txt";
            //name of the input file
        Vector myVector = new Vector();
            //myVector is used for traversals
        
        /***fill tree***/
        binTree.insert(new Item(1));
        binTree.insert(new Item(4));
        binTree.insert(new Item(9));
        binTree.insert(new Item(6));
        binTree.insert(new Item(3));
        binTree.insert(new Item(8));
        binTree.insert(new Item(7));
        binTree.insert(new Item(10));
        binTree.insert(new Item(5));
        binTree.insert(new Item(2));
        
        /***save to file***/
        System.out.println("Saving to file \"project 2 file.txt\".");
        try 
        {
            binTree.SaveToFile(binTree.root, fileName);
            System.out.println("File saved successfully.");
        } 
        catch (IOException e) 
        {
            System.err.println("Error saving file.");
        }
        
        /***Inorder traversal and output***/
        binTree.InOrder(binTree.root, myVector);
        System.out.println("Inorder traversal of tree:");
        for(int j = 0; j < 10; j++)
            System.out.println(myVector.elementAt(j));
        
        /***Preorder traversal and output***/
        myVector.removeAllElements();
        
        binTree.PreOrder(binTree.root, myVector);
        
        System.out.println("Preorder traversal of tree: ");
        for(int j = 0; j < myVector.size(); j++)
            System.out.println(myVector.elementAt(j));
        
        /***Postorder traversal and output***/
        myVector.removeAllElements();
        
        binTree.PostOrder(binTree.root, myVector);
        
        System.out.println("Postorder traversal of tree:");
        for(int j = 0; j < myVector.size(); j++)
            System.out.println(myVector.elementAt(j));
        
        /***Load from file***/
        //Note: loading from a file is not possible (at least to my knowledge)
        //from a generic context, so I placed the code in this main program
        //to allow the Integer type casting of a string and to allow it to
        //be inserted.
        System.out.println("Loading from file \"project 2 file.txt\".");
        try
	{
	BufferedReader in = new BufferedReader(new FileReader(fileName));
	
		binTree.BSTmakeEmpty();
		while (in.ready())
		{
                        Integer inData = Integer.valueOf(in.readLine());
			binTree.insert(new Item(inData));
		}
                in.close();
                System.out.println("File loaded sucessfully.");
	}
	catch(IOException e)
	{
            System.err.println("Error loading file.");
	}

        
        
        /***Deletion by Item***/
        try 
        {
            System.out.println("Delete the number 1");
            binTree.delete(new Item(1));
            //         System.out.println(binTree.retrieve(1));
            myVector.removeAllElements();
            
            System.out.println("Inorder Output to show that the number 1 has been deleted: ");
            binTree.InOrder(binTree.root, myVector);
            for(int j = 0; j < myVector.size(); j++) 
            {
                System.out.println(myVector.elementAt(j));
            }
        } 
        catch (TreeException t) 
        {
            System.out.println("Value 1 is not found in tree.");
        }
        
        /***Deletion by searchkey***/
        try 
        {
            binTree.delete(7);
            System.out.println("Deleting the value 7");
            myVector.removeAllElements();
            
            System.out.println("Inorder Output to show that 7 has been deleted: ");
            binTree.InOrder(binTree.root, myVector);
            for(int j = 0; j < myVector.size(); j++) 
            {
                System.out.println(myVector.elementAt(j));
            }
        } 
        catch (TreeException t) 
        {
            System.out.println("Value 7 is not found in tree.");
        }
        
        /***Calculate and display the height of the tree***/
        System.out.println("The height of the tree is " + binTree.CalcHeight(binTree.root));
    }//end main
}//end class
